#include <stdio.h>
#include <string.h>
#include <mysql/mysql.h>
#include <unistd.h>
#include <pthread.h>

#define MAX_SQL_SIZE 1024

void *thread1(void *arg)
{
    MYSQL *mysql = mysql_init(NULL);
}

void *thread2(void *arg)
{
    MYSQL *mysql = mysql_init(NULL);
}

int main(int argc, char **argv)
{
    // 1、创建的mysql的MYSQL
    MYSQL *mysql = mysql_init(NULL);

    char *sql = (char *)malloc(sizeof(char) * MAX_SQL_SIZE);
    memset(sql, 0, MAX_SQL_SIZE);

    const char *username = "root";
    const char *passwd = "1";
    const char *ip = "192.168.100.128";
    const char *db = "test";
    int port = 3306;

    // 1.1 设置断开连接之后自动重新连接
    int realconect = 1;
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &realconect);

    // 1.2 设置连接超时时间
    int timeout = 5;
    mysql_options(mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout);

    // 2、通过MYSQL句柄连接mysql server
    if (mysql_real_connect(mysql, ip, username, passwd, db, port, NULL, 0) == NULL)
    {
        printf("mysql real connect error:%s\n", mysql_error(mysql));
    }

    printf("mysql connect server Success!\n");

    const char * csname = "utf8";
    mysql_set_character_set(mysql, csname);

    strcpy(sql, "CREATE TABLE IF NOT EXISTS students (\
    student_id INT AUTO_INCREMENT PRIMARY KEY,\
    student_name VARCHAR(50) NOT NULL ,\
    student_age INT\
    ) ENGINE=InnoDB");

    if (mysql_real_query(mysql, sql, strlen(sql)) != 0)
    {
        printf("create table students error %s\n", mysql_error(mysql));
    }

    char sname[20] = "admin1";
    int age = 5;

    // for (int i = 0; i < 10; i++)
    // {
    //     memset(sql, 0, MAX_SQL_SIZE);
    //     sname[5]++;
    //     sprintf(sql, "INSERT INTO students(student_id, student_name, student_age) VALUES(%d, '%s', %d)",
    //             i + 1, sname, age);
    //     if (mysql_real_query(mysql, sql, strlen(sql)) != 0)
    //     {
    //         printf(" INSERT INTO students error %s\n", mysql_error(mysql));
    //     }
    // }

    // insert into, update set, delete from --- mysql_read_queue: 不需要有结果返回
    // select:需要查询结果；

    memset(sql, 0, MAX_SQL_SIZE);
    //strcpy(sql, "SELECT student_name, student_age FROM students");
    strcpy(sql, "SELECT * FROM student");
    if (mysql_real_query(mysql, sql, strlen(sql)) != 0)
    {
        printf(" Select students error %s\n", mysql_error(mysql));
    }

    MYSQL_RES *result = mysql_store_result(mysql);
    if (result == NULL)
    {
        printf(" Select get result students error %s\n", mysql_error(mysql));
    }

    int len = mysql_num_fields(result);
    MYSQL_FIELD * field; 
    for(int i = 0; i < len; i++)
    {
       field = mysql_fetch_field(result);
       printf("%s|", field->name);
    }
    printf("\n");
    

    MYSQL_ROW row;
    
    while ((row = mysql_fetch_row(result)) != NULL)
    {
        for (int i = 0; i < len; i++)
        {
            printf("%s|", row[i]);
        }
        printf("\n");
    }

    mysql_free_result(result);

    while (1)
    {
        if (mysql_ping(mysql) == 0)
        {
            printf("ping mysql Success!\n");
            sleep(1);
        }
        else
        {
            printf("ping mysql failed!\n");
            sleep(1);
        }
    }

    mysql_close(mysql);

    return 0;
}